package ${actionPackage};

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



import ${daoPackage}.admin.annotation.Menus;
import ${daoPackage}.admin.annotation.RequiredPermission;
import ${daoPackage}.admin.controller.mall.base.BaseController;
import ${daoPackage}.common.utils.ResponseUtil;
import ${daoPackage}.core.validator.Order;
import ${daoPackage}.core.validator.Sort;
import ${daoPackage}.${modular}.db.domain.${tableNameHump?cap_first};
import ${servicePackage}.I${tableNameHump?cap_first}SV;
import ${queryPackage}.${queryModel?cap_first};
/**
 * Copyright: Copyright (c) 2020  
 * ClassName: ${tableNameHump?cap_first}Controller.java
 * 
 * @Description: ${description}
 * 
 * @version: v1.0.0
 * @author: 盐
 * @date: ${.now?date}
 * 
 */ 
@RestController
@RequestMapping("/${rootRouter}/${alias}")
@Validated
public class ${tableNameHump?cap_first}Controller extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(${tableNameHump?cap_first}Controller.class);

	@Resource
	private I${tableNameHump?cap_first}SV ${tableNameHump}SV;

	
//	/** ${description} */
//	${permPrefix?upper_case}_${alias?upper_case}("${description}", "${permPrefix?lower_case}-${alias?lower_case}", "${alias?lower_case}", "/${permPrefix?lower_case}/","${modular}"),

	@GetMapping("/list")
	@RequiredPermission(value = "${permPrefix}:${alias}:list", button = "查询", menu = { Menus.${permPrefix?upper_case}_${alias?upper_case} })
	public Object list(${queryModel?cap_first}  ${queryModel?uncap_first} , @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order) {
		LOGGER.debug("搜索");
		List<${tableNameHump?cap_first}> list = ${tableNameHump}SV.queryList(${queryModel?uncap_first} ,  page,  limit,   sort,  order);
		
		
		
		return ResponseUtil.okList(list);
	}

	private Object validate(${tableNameHump?cap_first} ${tableNameHump}) {
	
			if (null ==  ${tableNameHump} ) {
				LOGGER.error("${description}不能为空");
				return ResponseUtil.badArgument();
			}
			<#list forms as row>
			<#if row.javaType == "String" >
			if (StringUtils.isBlank( ${tableNameHump}.get${row.humpName?cap_first}())) {
				LOGGER.error("${row.description}不能为空");
				return ResponseUtil.badArgument();
			}
			<#else >
			if (null != ${tableNameHump}.get${row.humpName?cap_first}()) {
				LOGGER.error("${row.description}不能为空");
				return ResponseUtil.badArgument();
			}
			</#if>
			</#list> 
		return null;
	}

	@PostMapping("/create")
	@RequiredPermission(value = "${permPrefix}:${alias}:create", button = "添加", menu = { Menus.${permPrefix?upper_case}_${alias?upper_case} })
	public Object create(@RequestBody ${tableNameHump?cap_first} ${tableNameHump}) {
		Object error = validate(${tableNameHump});
		if (error != null) {
			return error;
		}
		${tableNameHump}.setCompanyId(getAdminCompany());
		${tableNameHump}SV.add(${tableNameHump});
		return ResponseUtil.ok(${tableNameHump});
	}

	@GetMapping("/read")
	@RequiredPermission(value = "${permPrefix}:${alias}:read", button = "详情", menu = { Menus.${permPrefix?upper_case}_${alias?upper_case} })
	public Object read(@NotNull Integer id) {
		${tableNameHump?cap_first} ${tableNameHump} = ${tableNameHump}SV.findById(id);
		if (${tableNameHump}.getCompanyId() != getAdminCompany()) {
			return ResponseUtil.badArgument();
		}
		return ResponseUtil.ok(${tableNameHump});
	}

	@PostMapping("/update")
	@RequiredPermission(value = "${permPrefix}:${alias}:update", button = "编辑", menu = { Menus.${permPrefix?upper_case}_${alias?upper_case} })
	public Object update(@RequestBody ${tableNameHump?cap_first} ${tableNameHump}) {
		Object error = validate(${tableNameHump});
		if (error != null) {
			return error;
		}

		${tableNameHump?cap_first} old${tableNameHump?cap_first} = ${tableNameHump}SV.findById(${tableNameHump}.getId());
		if (old${tableNameHump?cap_first}.getCompanyId() != getAdminCompany()) {
			return ResponseUtil.badArgument();
		}
		if (${tableNameHump}SV.updateById(${tableNameHump}) == 0) {
			return ResponseUtil.updatedDataFailed();
		}
		return ResponseUtil.ok(${tableNameHump});
	}

	@PostMapping("/delete")
	@RequiredPermission(value = "${permPrefix}:${alias}:delete", button = "删除", menu = { Menus.${permPrefix?upper_case}_${alias?upper_case} })
	public Object delete(@RequestBody ${tableNameHump?cap_first} ${tableNameHump}) {
		Long id = ${tableNameHump}.getId();
		if (null == id) {
			return ResponseUtil.badArgument();
		}
		${tableNameHump?cap_first} old${tableNameHump?cap_first} = ${tableNameHump}SV.findById(id);
		if (old${tableNameHump?cap_first}.getCompanyId() != getAdminCompany()) {
			return ResponseUtil.badArgument();
		}
		${tableNameHump}SV.delete(id);
		return ResponseUtil.ok();
	}

}
